package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hmdp.utils.RedisConstants.FOLLOW_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private IUserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 判断是关注还是取关
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        if (user == null){
            return Result.ok();
        }
        String key =  FOLLOW_KEY+user.getId().toString();
        if (isFollow){
            Follow follow = new Follow();
            follow.setUserId(user.getId());
            follow.setFollowUserId(followUserId);
            boolean isSuccess = save(follow);
            if (isSuccess){
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }
            return Result.ok();
        }
        // 关注就是新增数据
        LambdaQueryWrapper<Follow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Follow::getUserId, user.getId()).eq(Follow::getFollowUserId, followUserId);
        boolean isRemove = remove(lambdaQueryWrapper);
        if (isRemove){
            stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
        }
        // 取关就是删除数据
        return Result.ok();
    }

    @Override
    public Result isFollow(Long id) {
        UserDTO user = UserHolder.getUser();
        if (user == null){
            return Result.ok();
        }
        LambdaQueryWrapper<Follow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Follow::getUserId, user.getId()).eq(Follow::getFollowUserId, id);
        int count = count(lambdaQueryWrapper);
        return Result.ok(count > 0);
    }

    @Override
    public Result followCommons(Long id) {
        Long userId = UserHolder.getUser().getId();
        String key1 = FOLLOW_KEY+userId;
        String key2 = FOLLOW_KEY+id;
        Set<String> commonFollowIds = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if (commonFollowIds==null||commonFollowIds.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        List<Long> commonFollowIdsList = commonFollowIds.stream().map(Long::valueOf).collect(Collectors.toList());
        List<User> users = userService.listByIds(commonFollowIdsList);
        List<UserDTO> userDTOS = users.stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
